/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yournamehere.guice;

import java.util.List;
import org.hibernate.Session;
import org.jvyaml.YAML;
import org.yournamehere.model.User;

/**
 *
 * @author odpsoft
 */
public class Client {

    private Service service;
    private Service2 service2;

    public Client() {
        this.service = ModuleFactory.getInstance().getInstance(Service.class);
        this.service2 = ModuleFactory.getInstance().getInstance(Service2.class);
    }

    public static void main(String[] ar) {
        Client c = new Client();
        c.pagedUser();
    }

    public void YAMLTest(){
        YAML.config();
        NamedUser user = new NamedUser();
        user.setName("name");
        user.setOld("old");
        System.out.println(YAML.dump(user));
    }
    
    private class NamedUser{
        String name;
        String old;

        public String getOld() {
            return old;
        }

        public void setOld(String old) {
            this.old = old;
        }
        
       void  setName (String m){
           name = m;
       }

        public String getName() {
            return name;
        }
       
    }
    
    public void finder() {
        Session session1 = ModuleFactory.getInstance().getInstance(Session.class);
        session1.beginTransaction();
        PersonAccess accessor = ModuleFactory.getInstance().getInstance(PersonAccess.class);
        List<User> ls = accessor.listAll();
        for (User us : ls) {
            System.out.println("name:" + us.getName());
        }
        
        session1.getTransaction().rollback();
    }

    public void save() {
        service.go();

        System.out.println(service2.say("minmin"));
        Session session1 = ModuleFactory.getInstance().getInstance(Session.class);


        try {
            User user = new User();
            user.setName("欧大鹏");
            session1.beginTransaction();
            session1.persist(user);
            session1.getTransaction().commit();

            System.out.print("save successful in version 8.1");

        } catch (Exception e) {
            session1.getTransaction().rollback();
            System.out.println("save failed in version 8.1");
            e.printStackTrace();
        }
    }
    
    public void findUser(){
        Session session1 = ModuleFactory.getInstance().getInstance(Session.class);
        session1.beginTransaction();
        PersonAccess accessor = ModuleFactory.getInstance().getInstance(PersonAccess.class);
        List<User> ls = accessor.find("minmin");
        for (User us : ls) {
            System.out.println("name:" + us.getName());
        }
        
        User user = accessor.findById(new Long(120));
        
        System.out.println("user name:"+user.getName());
        
        session1.getTransaction().rollback();
    }
    
    public void pagedUser(){
        Session session1 = ModuleFactory.getInstance().getInstance(Session.class);
        session1.beginTransaction();
        PersonAccess accessor = ModuleFactory.getInstance().getInstance(PersonAccess.class);
        List<User> ls = accessor.pagedUser("minmin",0, 3);
        for (User us : ls) {
            System.out.println("id:"+us.getId()+" name:" + us.getName());
        }
        
        List<User> ls2 = accessor.pagedUser("minmin",3, 3);
        for (User us : ls2) {
            System.out.println("id:"+us.getId()+" name:" + us.getName());
        }
        
        
        User user = accessor.findById(new Long(120));
        
        System.out.println("user name:"+user.getName());
        
        session1.getTransaction().rollback();
    }
}
